package com.tubang.logistics.controller.statistics;

import com.github.pagehelper.Page;
import com.tubang.common.util.ResultData;
import com.tubang.logistics.service.IStatisticsUserService;
import com.tubang.logistics.util.DateUtils;
import com.tubang.logistics.vo.statistics.UserTableVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("statistics/user")
public class StatisticsUserController {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IStatisticsUserService iStatisticsUserService;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listUserPercent(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean carrier) {
		try {
			Map<String, Object> result = iStatisticsUserService.listUserPercent(startTime, endTime, userId, carrier);
			return ResultData.result(true).setMsg("查询成功").setData(result);
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/table", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listUserTable(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean carrier,
			@RequestParam(required = false) String content,
			@RequestParam Integer page,
			@RequestParam Integer rows) {
		try {
			List<UserTableVO> userTableVOS = iStatisticsUserService.listTable(startTime, endTime, userId, carrier, content, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(userTableVOS).setTotalCount((int) ((Page) userTableVOS).getTotal());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/export", method = RequestMethod.POST)
	public @ResponseBody
	ResultData listUserTable(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean carrier,
			@RequestParam(required = false) String content,
			HttpServletResponse response) {
		try {
			HSSFWorkbook workbook = iStatisticsUserService.exportTable(startTime, endTime, userId, carrier, content);
			//拼接文件名
			//String yearMonthDayNow = DateUtils.getYearMonthDayNow();
			//String filename = "用户详情_" + yearMonthDayNow + ".xls";
			String filename = "用户详情.xls";

			//设置头信息
			response.setContentType("Application/msexcel");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1"));

			//文件下载
			OutputStream outputStream = response.getOutputStream();
			workbook.write(outputStream);
			return ResultData.result(true).setMsg("导出成功");
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("导出异常");
		}
	}
}
